package it.appcatalogobase;

import java.util.Iterator;
import java.util.List;

import it.appcatalogobase.R;
import it.appcatalogobase.R.id;
import it.appcatalogobase.R.layout;
import it.appcatalogobase.R.menu;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class ListProductsActivity extends ActionBarActivity {

	private ProdottoDataSource datasourcepr;
	private GridView gridView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_products);

//		if (savedInstanceState == null) {
//			getSupportFragmentManager().beginTransaction()
//					.add(R.id.container, new PlaceholderFragment()).commit();
//		}
		datasourcepr = new ProdottoDataSource(this);
	    datasourcepr.open();
		if(getIntent().getStringExtra("type").equals("all")){
			
//		    datasourcepr.createProdotto("Caffè amaro", "amaro amarissimo", "Caffè", "caffe_amaro");
//		    datasourcepr.createProdotto("Caffè dolce", "dolce dolcissimo", "Caffè", "caffe_dolce");
//		    datasourcepr.createProdotto("Birra", "boooooooo", "altro", "birra");
	
		    List<Prodotto> valuesPr = datasourcepr.getAllProdotti();
		    Iterator<Prodotto> it = valuesPr.iterator();
		    final String[] names = new String[valuesPr.size()];
		    String[] ids = new String[valuesPr.size()];
		    final String[] images = new String[valuesPr.size()];
		    final String[] descr = new String[valuesPr.size()];
		    int counter=0;
		    while(it.hasNext()){
		    	Prodotto p = it.next();
		    	names[counter]=p.getNome();
		    	ids[counter]=String.valueOf(p.getId());
		    	images[counter]=p.getImmagine();
		    	descr[counter]=p.getDescrizione();
		    	counter++;
		    }
		    if(counter==0){
		    	Toast t = Toast.makeText(this, "NESSUN PRODOTTO PRESENTE NEL CATALOGO", Toast.LENGTH_LONG);
		    	t.setGravity(Gravity.CENTER, 0, 0);
		    	t.show();
		    }
		    gridView = (GridView) findViewById(R.id.gridview);
		    gridView.setAdapter(new ImageAdapter(this, names,ids,images));
		    gridView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
					Intent i = new Intent(ListProductsActivity.this,ProductDettaglioActivity.class);
					i.putExtra("name", names[position]);
					i.putExtra("des", descr[position]);
					i.putExtra("imm", images[position]);
					startActivity(i);				}
			});
		}else if(getIntent().getStringExtra("type").equals("cat")){
			String cat = getIntent().getStringExtra("cat");
			Long id = getIntent().getLongExtra("id", 1);
			List<Prodotto> valuesPr = datasourcepr.getAllProdottiFromCat(cat);
			Iterator<Prodotto> it = valuesPr.iterator();
		    final String[] names = new String[valuesPr.size()];
		    String[] ids = new String[valuesPr.size()];
		    final String[] images = new String[valuesPr.size()];
		    final String[] descr = new String[valuesPr.size()];
		    int counter=0;
		    while(it.hasNext()){
		    	Prodotto p = it.next();
		    	names[counter]=p.getNome();
		    	ids[counter]=String.valueOf(p.getId());
		    	images[counter]=p.getImmagine();
		    	descr[counter]=p.getDescrizione();
		    	counter++;
		    }
		    if(counter==0){
		    	Toast t = Toast.makeText(this, "NESSUN PRODOTTO PRESENTE PER LA CATEGORIA SELEZIONATA", Toast.LENGTH_LONG);
		    	t.setGravity(Gravity.CENTER, 0, 0);
		    	t.show();
		    }
		    gridView = (GridView) findViewById(R.id.gridview);
		    gridView.setAdapter(new ImageAdapter(this, names,ids,images));
		    gridView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
					Intent i = new Intent(ListProductsActivity.this,ProductDettaglioActivity.class);
					i.putExtra("name", names[position]);
					i.putExtra("des", descr[position]);
					i.putExtra("imm", images[position]);
					startActivity(i);
//					i.putExtra("id", ((TextView) v.findViewById(R.id.grid_item_id)).getText().toString());
				}
			});
		}
	    
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_products, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
//	public static class PlaceholderFragment extends Fragment {
//
//		public PlaceholderFragment() {
//		}
//
//		@Override
//		public View onCreateView(LayoutInflater inflater, ViewGroup container,
//				Bundle savedInstanceState) {
//			View rootView = inflater.inflate(R.layout.fragment_list_products,
//					container, false);
//			return rootView;
//		}
//	}

}
